package com.xsocket.study.servicehandler;

import java.io.IOException;
import java.nio.BufferUnderflowException;
import java.nio.channels.ClosedChannelException;

import org.xsocket.MaxReadSizeExceededException;
import org.xsocket.connection.BlockingConnection;
import org.xsocket.connection.IBlockingConnection;
import org.xsocket.connection.IConnectHandler;
import org.xsocket.connection.IDataHandler;
import org.xsocket.connection.INonBlockingConnection;

public class EchoHandler implements IDataHandler,IConnectHandler {
	
	public EchoHandler(){
		
	}
	
	/**
	 * 即这个方法不光是说当接收到一个新的网络包的时候会调用而且如果有新的缓存存在的时候也会被调用。而且  
     *The onData will also be called, if the connection is closed当连接被关闭的时候也会被调用的! 
	 */
	@Override
	public boolean onData(INonBlockingConnection ibc) throws IOException,
			BufferUnderflowException, ClosedChannelException,
			MaxReadSizeExceededException {
		// TODO Auto-generated method stub
		System.out.println("服务器端ondata开始............");
		String readString = ibc.readStringByDelimiter("\r\n");
		System.out.println("服务器端读到的数据：  " + readString);
		ibc.write("服务器端返回的数据：  " + readString + "\r\n");
		
		return true;
	}

	@Override
	public boolean onConnect(INonBlockingConnection ibc) throws IOException,
			BufferUnderflowException, MaxReadSizeExceededException {
		// TODO Auto-generated method stub
		
		
		
	    //IBlockingConnection bc = new BlockingConnection(ibc); 
		
		System.out.println("....链接完成");

		return true;
	}

}
